package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.tamartech.greendb.loader.ElectoralRollLoader;

// class to initially import Jonathan's spreadsheet. Unlikely ever to need it again
public class JESpreadsheet
{
    private static Logger logger = Logger.getLogger (JESpreadsheet.class);
    private static SimpleDateFormat formatRollDate = new SimpleDateFormat ("dd/MM/yyyy");
    private static SimpleDateFormat format18thDate = new SimpleDateFormat ("dd/MM/yy");
    
    private static final int COL_ADDRESS5 = 21;
    private static final int COL_ADDRESS6 = 22;

    
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        String wardName = args[1];
        Date rollDate = formatRollDate.parse(args[2]);
        File logDir = new File (args[3]);
        
        PrintWriter newAddressLog = new PrintWriter (new File (logDir, "NewAddresses-" + wardName + ".log"));
        PrintWriter newPersonLog = new PrintWriter (new File (logDir, "NewPersons-" + wardName + ".log"));
	    
        ElectoralRollLoader loader = new ElectoralRollLoader (newPersonLog, newAddressLog);
        loader.begin(wardName, rollDate);
        
        LabeledCSVParser csv = new LabeledCSVParser (new CSVParser (new FileInputStream (inFile)));
        
        
        for (;;)
        {
            // not all cols are labelled
            String[] line = csv.getLine();

            if (line == null)
                break;
            
            if (csv.getValueByLabel("U").trim().equals("U"))
                // more headings - ignore
                continue;
            
            String ref1 = csv.getValueByLabel("code").trim();
            String ref2 = csv.getValueByLabel("code 2").trim();
            
            loader.beginEntry(ref1, ref2);
            
            loader.putName (csv.getValueByLabel("first names").trim(), csv.getValueByLabel("surname").trim());
            loader.putAddress(csv.getValueByLabel("address 1").trim(), 
                              csv.getValueByLabel("address 2").trim(), 
                              csv.getValueByLabel("address 3").trim(), 
                              csv.getValueByLabel("postcode").trim(), 
                              line[COL_ADDRESS5].trim(), 
                              line[COL_ADDRESS6].trim());
            
            Date date18th = null;
            String str18th = csv.getValueByLabel("18th bday").trim();
            if (str18th.length() > 0)
                date18th = format18thDate.parse(str18th);
            
            if (date18th != null)
                loader.put18thBirthday (date18th);
            
            loader.endEntry();
        }
        
        csv.close();

        loader.end();
        
        newAddressLog.close();
        newPersonLog.close();
	}	
} 